﻿/**
 * ...
 * @author nado
 */

package ;

import php.db.Manager;
import db.Channel;
import db.Category;
import db.Item;
import db.ItemTags;
import db.ChannelTags;
import db.ChannelManager;
import php.Lib;

class Inserter 
{
	static var rss : RSS;
	
	public static function Connect()
	{
		try 
		{
			var connection = php.db.Mysql.connect(Configuration.getDbParams());
			connection.request("SET NAMES 'utf8';");
			Manager.cnx = connection;
		} catch (e: Dynamic) { Lib.print(e); return; }
	}
/*	public static function CollectCategories(rss: RSS)  что это? для чего? работает?
	{
		var item: Item;
		var ItemCats = new Array<String>();
		var item_title = new Array<String>();
		var item_descr = new Array<String>();
		var k, p,i: Int;
		var TempStr: String;
		
		 k = 0;  p = 0; i = 0;
		 var k1, p1,i1: Int;
		var TempStr1: String;
		 k1 = 0;  p1 = 0; i1 = 0;
		var ItemArr: Array<Item>;
		ItemArr = rss.getItems();
	  for (item in ItemArr) 
	  {
		p = item.name.indexOf(" ", k);
		  if (k != -1)
		    {
		      TempStr = item.name.substr(p, k); 
			  k = p;
			  item_title[i] = TempStr;
			  i++;
			}
	  }
	   for (item in ItemArr) 
	  {
		p1 = item.description.indexOf(" ", k1);
		  if (k1 != -1)
		    {
		      TempStr1 = item.description.substr(p1, k1); 
			  k1 = p1;
			  item_descr[i1] = TempStr1;
			  i1++;
			}
	  }	
	  for (i in 0 ... item_title.length) {
		for (i1 in 0 ...item_descr.length )
		    if (item_title[i] == item_descr[i1])
		      ItemCats.push(item_title[i]);
		  }
	}
	*/
	static public function addChannelWithTags(source:String, Tags:Array<String>)
	{
		initRss(source);
		var rss 			: RSS		 = rss;
 		var channel_id		: Int 		 = GetChannelId(rss);
		var channel_tag		: ChannelTags;
		var cat				: String;
		var cat_id			: Int;
		
		for (cat in Tags)
		{
			cat_id			 = getCatId(cat);
			channel_tag 	 = new ChannelTags(cat_id, channel_id);
			channel_tag.insert();
		}
	}
	
	static public function updateChannel(source:String)
	{
		var rss 			: RSS 			= rss;
		var channel_id  	: Int           = 0;
		var cat_id			: Int			= 0;
		var item_id			: Int;	
		var item			: Item;
		var item_tag		: ItemTags      = new ItemTags(0, 0);
		var channel			: Channel 		= rss.getChannel();
		var channelmanager  : Channel		= ChannelManager.GetByName(channel.name);
		var rset	   					    = Manager.cnx.request("SELECT * FROM channels WHERE name = '" + channel.name +"';");
		
		for (row in rset) 
			channel_id = row.id;
			
		rss.setChannel_id(channel_id);
		
			for (item in rss.getItems())
			{
				if (item.date.getTime() > channelmanager.date.getTime()  )
				{
					item.insert();
					item_id = item.id;
					
					if (item.category.length != 0)
					{
						for ( category in item.category )
						{
							if (category != "")
								{	
									cat_id = getCatId(category); 
								}
						item_tag.id_cat = cat_id;
						item_tag.id_item = item.id;
						item_tag.insert();
						}
					}						
				}	
			}				
		channel.date = Date.now();
		//channel.update();	
	}
	
	static function getCatId(name:String):Int
	{
		var rset = Manager.cnx.request("SELECT * FROM categories WHERE name = '" + name + "';"); 
		var cat = new Category(name);
		for ( row in rset ) return row.id;
		
		cat.insert();
		return cat.id;	
	}
	
	static function GetChannelId(rss: RSS):Int
	{
		var channel : Channel = rss.getChannel();
		var rset    = Manager.cnx.request("SELECT * FROM channels WHERE name = '" + channel.name +"';");	
		for (row in rset) return row.id;
		channel.insert();
		return channel.id;
	}

	static function initRss(source : String)
	{
		rss = new RSS(source);
	}
	
	public function new() 
	{
		
	}
	
}